package org.bjf.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.MapUtils;
import org.bjf.modules.core.bean.Query;

import java.util.List;
import java.util.Map;

/**
 * @author bjf
 * @date 2017/11/27
 */
public class PageUtil {

    /**
     * 取mybatis-plus分页对象
     */
    public static Page getPage(Query query) {
        return new Page(query.getPage(), query.getPageSize());
    }

    /**
     * 取mybatis-plus分页对象
     */
    public static Page getPage(Map<String, String> params) {
        int page = MapUtils.getInteger(params, "page", 1);
        int pageSize = MapUtils.getInteger(params, "pageSize", 20);
        return new Page(page, pageSize);
    }

    /**
     * 内存分页
     */
    public static <T> List<T> page(List<T> source, int page, int size) {
        if (page <= 0) {
            page = 1;
        }
        int from = (page - 1) * size;
        int to = page * size;
        if (to > source.size()) {
            to = source.size();
        }
        List<T> ts = source.subList(from, to);

        return ts;
    }
}
